package servlet;

import dao.CartDAO;
import entity.Cart;
import entity.PageBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "cartPage", value = "/cart/page")
public class CartPageServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 接受pageNum pageSize
        String num = req.getParameter("pageNum");
        String size = req.getParameter("pageSize");
        int pageNum = Integer.parseInt(num);
        int pageSize = Integer.parseInt(size);
        //2. 将pageNum pageSize === skip 和limit
        int skip = (pageNum-1)*pageSize;
        int limit = pageSize;
        CartDAO cartDAO = new CartDAO();
        List<Cart> list = cartDAO.findPage(skip,limit);
        long total = cartDAO.getCount();
        PageBean<Cart> page = new PageBean<>();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        page.setList(list);
        page.setTotal(total);
        page.setPageTotal(total,pageSize);
        req.setAttribute("page",page);
        req.getRequestDispatcher("/cart/List.jsp").forward(req,resp);
        System.out.println("分页展示成功");
    }
}
